package cn.dakaqi.volunteer.tag;

import cn.dakaqi.entities.PlatformGroup;
import cn.dakaqi.entities.PlatformGroupVolunteer;
import cn.dakaqi.entities.Volunteer;
import cn.dakaqi.utils.Clock;
import cn.dakaqi.utils.DKQConstant;
import cn.dakaqi.utils.exception.ServiceRuntimeException;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import java.util.List;
import java.util.Map;

/**
 * Created by chunyang on 2016/6/5.
 */
@Service(value = "platformGroupService")
@Transactional
public class PlatformGroupServiceImpl implements PlatformGroup2Service
{
    @Autowired
    PlatformGroupDao platformGroupDao;
    @Autowired
    PlatformGroupVolunteerService platformGroupVolunteerService;

    @Override
    public void save(long groupId, long platformId, String parentCode, int status,List<Volunteer> volunteers) throws ServiceRuntimeException
    {
        PlatformGroup platformGroup = new PlatformGroup();
        platformGroup.setStatus(status);
        platformGroup.setCreateTime(Clock.DEFAULT.getCurrentDate());
        platformGroup.setGroupId(groupId);
        platformGroup.setPlatformId(platformId);
        platformGroup.setParentCode(parentCode);
        this.save(platformGroup,volunteers);
    }

    @Override
    public void save(PlatformGroup platformGroup,List<Volunteer> volunteers) throws ServiceRuntimeException
    {
        //检查当前社团是否已挂靠了当前平台
        PlatformGroup temp = findByGroupAndPlat(platformGroup.getGroupId(),platformGroup.getPlatformId());
        if(null != temp)
            throw new ServiceRuntimeException("当前社团已挂靠了当前平台");

        platformGroupDao.save(platformGroup);
        /*
        //将当前社团中的所有成员挂靠到指定的平台下
        if(null != volunteers && volunteers.size()>0)
        {
            for(Volunteer volunteer:volunteers)
            {
                PlatformGroupVolunteer platformGroupVolunteer = new PlatformGroupVolunteer();
                platformGroupVolunteer.setGroupId(platformGroup.getGroupId());
                platformGroupVolunteer.setCreateTime(Clock.DEFAULT.getCurrentDate());
                platformGroupVolunteer.setPlatformId(platformGroup.getPlatformId());
                platformGroupVolunteer.setVolunteerId(volunteer.getId());
                platformGroupVolunteer.setStatus(platformGroup.getStatus());
                platformGroupVolunteerService.save(platformGroupVolunteer);
            }
        }
        */
    }

    @Override
    public void update(PlatformGroup platformGroup) throws ServiceRuntimeException
    {
        platformGroupDao.save(platformGroup);
    }

    @Override
    public void delGroupAndPlat(long groupId, long platformId) throws ServiceRuntimeException
    {
        //删除社团挂靠关系
        platformGroupDao.delGroupAndPlat(groupId,platformId);
        //删除社团成员的挂靠关系
        platformGroupVolunteerService.delGroupAndPlat(groupId,platformId);
    }

    @Override
    public void delById(long id) throws ServiceRuntimeException
    {
        PlatformGroup pg = this.platformGroupDao.findOne(id);
        delGroupAndPlat(pg.getGroupId(),pg.getPlatformId());
    }

    @Override
    public List<PlatformGroup> findByGroup(long groupId) throws ServiceRuntimeException
    {
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("groupId", new SearchFilter("groupId", SearchFilter.Operator.EQ, groupId));
        Specification<PlatformGroup> spec = DynamicSpecifications.bySearchFilter(filters.values(), PlatformGroup.class);
        return this.platformGroupDao.findAll(spec);
    }

    @Override
    public List<PlatformGroup> findByPlat(long platformId) throws ServiceRuntimeException
    {
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("platformId", new SearchFilter("platformId", SearchFilter.Operator.EQ,platformId));
        Specification<PlatformGroup> spec = DynamicSpecifications.bySearchFilter(filters.values(), PlatformGroup.class);
        return this.platformGroupDao.findAll(spec);
    }

    @Override
    public PlatformGroup findByGroupAndPlat(long group, long platfrom) throws ServiceRuntimeException
    {
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("groupId", new SearchFilter("groupId", SearchFilter.Operator.EQ, group));
        filters.put("platformId", new SearchFilter("platformId", SearchFilter.Operator.EQ,platfrom));
        Specification<PlatformGroup> spec = DynamicSpecifications.bySearchFilter(filters.values(), PlatformGroup.class);
        List<PlatformGroup> list = this.platformGroupDao.findAll(spec);
        if(null != list && list.size()>0)
            return list.get(0);

        return null;

    }
}
